package com.dense.kuiniu.floatcabinet.mvp.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.floatcabinet.R;
import com.google.android.material.textfield.TextInputEditText;

import java.util.Objects;

/**
 * @author Administrator
 * @date 2020/10/28
 */
public class DialogAdminLogin extends Dialog {
    private OnLoginListener loginListener;

    public DialogAdminLogin(@NonNull Context context, OnLoginListener loginListener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_admin_login, null);
        TextInputEditText etUser = view.findViewById(R.id.et_user);
        TextInputEditText etPassword = view.findViewById(R.id.et_password);

        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String user = Objects.requireNonNull(etUser.getText()).toString();
                    String password = Objects.requireNonNull(etPassword.getText()).toString();
                    if (check(user, password)) {
                        if (loginListener != null) {
                            loginListener.onLogin(user, password);
                            dismiss();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showShort("数据错误");
                }

            }
        });

        setContentView(view);
    }

    private boolean check(String user, String pwd) {

//        if (user.isEmpty()) {
//            ToastUtils.showShort("用户名不能为空!");
//            return false;
//        }
//        if (pwd.isEmpty()) {
//            ToastUtils.showShort("密码不能为空!");
//            return false;
//        }
        return true;
    }

    @Override
    public void show() {
        super.show();
        measureSize();
    }

    protected void measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.shape_transparent));
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (ScreenUtils.getScreenWidth() * 0.85); // 宽度
            lp.height = (int) (ScreenUtils.getScreenHeight() * 0.54); // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
            dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        }
    }

    public interface OnLoginListener {
        void onLogin(String user, String password);
    }

}